package com.sky.service.impl;

import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.sky.constant.MessageConstant;
import com.sky.constant.StatusConstant;
import com.sky.dto.SetmealDTO;
import com.sky.entity.Setmeal;
import com.sky.entity.SetmealDish;
import com.sky.exception.BaseException;
import com.sky.exception.DeletionNotAllowedException;
import com.sky.mapper.SetmealDishMapper;
import com.sky.mapper.SetmealMapper;
import com.sky.result.PageResult;
import com.sky.service.SetmealService;
import com.sky.vo.SetmealVO;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

/**
 * 类描述：
 */
@Service
public class SetmealServiceImpl implements SetmealService {

    @Autowired
    private SetmealMapper setmealMapper;
    @Autowired
    private SetmealDishMapper setmealDishMapper;
    @Override
    @Transactional
    public void save(SetmealDTO setmealDTO) {
        Setmeal setmeal =new Setmeal();
        BeanUtils.copyProperties(setmealDTO, setmeal);
        setmeal.setStatus(StatusConstant.DISABLE);
        setmealMapper.save(setmeal);

        Long setmealId = setmeal.getId();
        List<SetmealDish> setmealDishes = setmealDTO.getSetmealDishes();
        if (setmealDishes != null && setmealDishes.size() > 0) {
            for (SetmealDish setmealDish : setmealDishes) {
                setmealDish.setSetmealId(setmealId);
            }
            setmealDishMapper.saveBatch(setmealDishes);
        }
    }

    @Override
    public PageResult page(Integer page, Integer pageSize, Integer categoryId, String name, Integer status) {
        PageHelper.startPage(page, pageSize);
        Page<SetmealVO> p = setmealMapper.page(categoryId, name, status);
        return new PageResult(p.getTotal(), p.getResult());
    }

    @Override
    public SetmealVO getById(Integer id) {
        SetmealVO setmealVO = setmealMapper.getById(id);

        List<SetmealDish> setmealDishes=setmealDishMapper.getSetmealDishes(id);
        setmealVO.setSetmealDishes(setmealDishes);
        return setmealVO;
    }

    @Override
    public void update(SetmealDTO setmealDTO) {
        Setmeal setmeal =new Setmeal();
        BeanUtils.copyProperties(setmealDTO, setmeal);
        setmealMapper.update(setmeal);

        Long setmealId = setmealDTO.getId();
        //删掉原来关联的菜品
        setmealDishMapper.deleteBySetmealId(setmealId);
        //添加新的关联菜品
        List<SetmealDish> setmealDishes = setmealDTO.getSetmealDishes();
        //遍历新套餐集合中的所有菜品，为每一个菜品设置套餐id
        if (setmealDishes != null && setmealDishes.size() > 0) {
            for (SetmealDish setmealDish : setmealDishes) {
                setmealDish.setSetmealId(setmealId);
            }
        }
        //批量保存新菜品到套餐中
        setmealDishMapper.saveBatch(setmealDishes);
    }

    @Transactional
    @Override
    public void delete(List<Long> ids) {
        //起售中的套餐不能删除
        for (Long id : ids) {
            if(setmealMapper.status(id)==StatusConstant.ENABLE){
                throw new DeletionNotAllowedException(MessageConstant.SETMEAL_ON_SALE);
            }
        }
        //删除套餐，同时删除套餐和菜品的关联数据
        setmealMapper.delete(ids);
        setmealDishMapper.deleteBySetmealIds(ids);
    }

    @Override
    public void startOrStop(Integer status, Long id) {
        //起售套餐时，判断套餐内是否有停售菜品，有停售菜品提示"套餐内包含未启售菜品，无法启售"
        if(status== StatusConstant.ENABLE){
            if(setmealDishMapper.countBySetmealId(id) != 0){
                throw new BaseException(MessageConstant.SETMEAL_ENABLE_FAILED);
            }
        }

        Setmeal setmeal = Setmeal.builder().id(id).status(status).build();
        setmealMapper.update(setmeal);
    }

    @Override
    public List<Setmeal> getSetmealByCategoryId(Integer categoryId) {
        List<Setmeal> setmeals=setmealMapper.getSetmealByCategoryId(categoryId);
        return setmeals;
    }

    @Override
    public List<SetmealDish> getDishBySetmealId(Integer id) {
        List<SetmealDish> setmealDishes = setmealDishMapper.getSetmealDishes(id);
        return setmealDishes;
    }


}
